//
//  CZPhotoBrowserDismissAnimation.swift
//  GZWeibo666
//
//  Created by iOS on 15/12/9.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class CZPhotoBrowserDismissAnimation: NSObject, UIViewControllerAnimatedTransitioning {
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.25
    }
    
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        // 获取到modal的控制器
        let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) as! CZPhotoBrowserViewController
        
        // 获取modal出来的控制器的view
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
        
        // 获取到关闭时的过渡视图
        let tempImageView = fromVC.dismissTempImageView()
        
        // 添加到容器视图
        transitionContext.containerView()?.addSubview(tempImageView)
        
        // 隐藏collectionView
        fromVC.collectionView.hidden = true
        
        // 淡出的动画
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            // 缩放到缩略图的位置
            tempImageView.frame = fromVC.dismissTargetRect()
            
            fromView?.alpha = 0
            }) { (_) -> Void in
                transitionContext.completeTransition(true)
        }
    }
}
